//revised
//Developing a counting system, scores K-10, Aces, 2-9
//adusts pot
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
import java.awt.*;
import java.awt.event.*;

public class BJ11 extends java.applet.Applet  implements ActionListener
{

	Label label1=new Label("Dealer");
	Label label2=new Label("$1000");
	Label label3=new Label("Player");
	Label label4=new Label("$1000");
	Label label5=new Label(" Pot ");
	Label label6=new Label(" $0  ");
	Label label7=new Label("          ");
	Button button1 = new Button("Deal");
	Button button2 = new Button("Hit me");
	Button button3 = new Button("Hold");

	//TextField text1 = new TextField("             ");

	Image [] Player1pic= new Image [5] ;
	Image [] Player2pic= new Image [5] ;
	int [] hearts = new int [13]; // this does an array
	int [] diamonds = new int [13];
	int [] clubs = new int [13];
	int [] spades = new int [13];
	String card=" ";
	int [] Player1=new int[5];
	int [] Player2=new int[5];
	String [] Player1Cards=new String[5];
	String [] Player2Cards=new String[5];

	String suit=" ";
	String value=" ";
	Random Generate1= new Random();
	int  x;
	int  y;
	int OK=1;

	int p=0;
	int d=0;
	int PlayerCash=1000;
	int DealerCash=1000;
	int PotCash=0;
	int GameStarted=0;
	int playerTotal; //the total of the cards held by player
	int dealerTotal; //the total of the cards held by dealer
	int count=0;
	//******************
	//temp variables used to test stuff
	int ii=0;
	//temp variables used to test stuff
    //******************

	//FlowLayout lm = new FlowLayout(FlowLayout.CENTER);

	public void init()
	{
		//button1.setLocation (100, 100);

		 add(label1);
		 add(label2);
		 add(label3);
		 add(label4);
		 add(label5);
		 add(label6);
		 add(label7);
		 add(button1);
		 add(button2);
		 add(button3);
		 		 button1.addActionListener(this);
		 		 button2.addActionListener(this);
		          button3.addActionListener(this);


	int i;
    for ( i=0; i<5; i++)  //picks 5 unique cards for player1 or dealer
	{
		do
		{
			card=card_maker();  //calls the next method card_maker
			OK=notAlreadyPicked();

		}while(OK == 0);
		Player1pic[i] = getImage(getCodeBase(),card); //mypic does make it to repaint
	    Player1[i]=y;  //new gives dealer his cards
		Player1Cards[i]=card;

	}
	for (i=0; i<5; i++)  //picks 5 unique cards for player2 or the player
	{
		do
		{
			card=card_maker();  //calls the next method card_maker
			OK=notAlreadyPicked();

		}while(OK == 0);
		//System.out.print(Player2[1]);
		Player2pic[i] = getImage(getCodeBase(),card);
		Player2[i]=y;  //new gives player his cards....mistake made earlier here
	    Player2Cards[i]=card;
		//******************************************************
		//routine allows me to set player cards to test for stuff
		/*
		Player2pic[0]=getImage(getCodeBase(),"C01.jpg");
		Player2pic[1]=getImage(getCodeBase(),"C02.jpg");
		Player2pic[0]=getImage(getCodeBase(),"C03.jpg");
		Player2pic[1]=getImage(getCodeBase(),"C04.jpg");
		Player2pic[0]=getImage(getCodeBase(),"C05.jpg");
		Player2[0]=1;
		Player2[1]=2;
		Player2[2]=3;
		Player2[3]=4;
		Player2[4]=5;
		Player2Cards[0]="C01.jpg";
		Player2Cards[1]="C02.jpg";
		Player2Cards[2]="C03.jpg";
		Player2Cards[3]="C04.jpg";
		Player2Cards[4]="C05.jpg";
		*/
	    //routine allows me to set player cards to test for stuff
		//******************************************************
	}
	}
	public String card_maker()  //returns a possible card string (picture file)
	{//variables in init are not sent down to here



	y=(int)(Generate1.nextDouble()*13)+1;
	x=(int)(Generate1.nextDouble()*4)+1; //4because 4 suits
	//******************************************************
				//routine allows me to set player cards to test for stuff				//playerTotal=2;
				//tempTotal=2;
				//tempVal=1;	            ii=ii+1;
				//dealer cards				if (ii==1)
				{					y=6;
					x=1;				}				if (ii==2)
				{					y=2;
					x=1;				}				if (ii==3)
				{					y=3;
					x=1;				}				if (ii==4)
				{					y=4;
					x=1;				}				if (ii==5)
				{					y=5;
					x=1;				}
				//***********				//player cards				if (ii==6)
				{					y=1;
					x=3;				}				if (ii==7)
				{					y=11;
					x=3;				}				if (ii==8)
				{					y=2;
					x=3;				}				if (ii==9)
				{					y=1;
					x=4;				}				if (ii==10)
				{					y=1;
					x=2;				}
				System.out.print(x+ " ");				//routine allows me to set player cards to test for stuff
				//******************************************************
	if (x==1)
		suit="H"; //suit is hearts
	if (x==2)
		suit="D"; //suit is diamonds
	if (x==3)
		suit="C"; //suit is clubs
	if (x==4)
		suit="S"; //suit is spades
	if(y==1)
		value="01";
	if(y==2)
		value="02";
	if(y==3)
		value="03";
	if(y==4)
		value="04";
	if(y==5)
		value="05";
	if(y==6)
		value="06";
	if(y==7)
		value="07";
	if(y==8)
		value="08";
	if(y==9)
		value="09";
	if(y==10)
		value="10";
	if(y==11)
		value="11";
	if(y==12)
		value="12";
	if(y==13)
		value="13";
	card=suit+value + ".jpg";
	return card;
	}
	public int notAlreadyPicked() //selects a unique card
	{

		if (suit=="C")
		{
			if (clubs[y-1] != 1)//this card is a good choice
			{
				clubs[y-1] =1;
				OK =1;
			}
		    else
			{
				OK=0;
			}
		}
		if (suit=="H")
		{
			if (hearts[y-1] != 1) //this card is a good choice
			{
				hearts[y-1] =1;
				OK =1;
			}
		    else
			{
				OK=0;
			}
		}
		if (suit=="S")
		{
			if (spades[y-1] != 1)  //this card is a good choice
			{
				spades[y-1] =1;
				OK =1;
			}
		    else
			{
				OK=0;
			}
		}
		if (suit=="D")
		{
			if (diamonds[y-1] != 1)  //this card is a good choice
			{
				diamonds[y-1] =1;
				OK =1;
			}
		    else
			{
				OK=0;
			}
		}
		return OK;
	}
	public void paint(Graphics screen)
	{	label1.setBackground(Color.white);
		label2.setBackground(Color.white);
		label3.setBackground(Color.white);
		label4.setBackground(Color.white);
		label5.setBackground(Color.white);
		label6.setBackground(Color.white);

		label1.setLocation(62, 160);
		label2.setLocation(65, 190);   //dealer cash
		button1.setLocation (120, 190);
		button2.setLocation (168, 200);
		button3.setLocation (223, 190);
		label3.setLocation(273, 160);
		label4.setLocation(276, 190);  //player cash
		label5.setLocation(175, 140);
		label6.setLocation(175, 168);  //pot cash
		label7.setLocation(170, 120);  //win/lose label

	    int	iWidth =71 ;//71 pixelsPlayer1pic[0].getWidth(this);
		int iHeight=96;  //96 pixelsPlayer1pic[0].getHeight(this);
		int xPos=0;


		for (int i=0; i<p; i++)  //player is Player2
		{
		   screen.drawImage(Player2pic[i], i*(iWidth+10), 250, this);
		}
		for (int i=0; i<d; i++)  //dealer is Player1
		{
			screen.drawImage(Player1pic[i], i*(iWidth+10),10, this);
		}

	}
	public void Reshuffle() //can only go 5 times without zeroing things
	{
		for (int i=0; i<5;i++)
		{
			clubs[i]=0;
			spades[i]=0;
			diamonds[i]=0;
			hearts[i]=0;
		}
	}

public void actionPerformed(ActionEvent event)
	{
	  String textRead;
	  System.out.println(event);
	  Object source = event.getSource();
      if (source == button1)
		{
			if (GameStarted==0)
			{				GameStarted=1;
				PlayerCash=PlayerCash-50;
				DealerCash=DealerCash-50;				PotCash=100;
				label4.setText((String.valueOf(PlayerCash)));  //correctly converts an int to a String
				label2.setText((String.valueOf(DealerCash)));
				label6.setText((String.valueOf(PotCash)));				//suit ="S";
				//Player2[0]=10;				//Player2[1]=11;
				if (Player2[0]+Player2[1]==21)  //21 scored so black jack is possible for player to win
				{					if (suit.equals("S"))    //Blackjack has occurred with spades player wins					{						label7.setText("You Win");
						PlayerCash=PlayerCash+100;						label4.setText(String.valueOf(PlayerCash));
					}					if (suit.equals("C"))	//Blackjack has with clubs occurred player wins					{						label7.setText("You Win");
						PlayerCash=PlayerCash+100;						label4.setText(String.valueOf(PlayerCash));					}
				}				p=2;		//gives player 2 cards				d=2;		//gives dealer 2 cards				//*****************//scores the cards of the player
				int tempTotal=0;
				int tempVal=0;
				int numAces=0;				for (int i=0; i<2; i++)				{

					if (Player2[i]==1)					{
						tempVal=11;
						numAces=numAces+1; //counts the number of aces to adjest scores later					}
					if (Player2[i]>9)
						tempVal=10;
					if ((Player2[i]>1) && (Player2[i]<10))
						tempVal=Player2[i];

					tempTotal=tempTotal +tempVal; //player2 is the player, player1 the dealer

				}				playerTotal=tempTotal;				label7.setText(String.valueOf(playerTotal));



				//*****************				repaint();  //calls the paint command now modified with the commands above!			}
		}
		if (source==button2)	//hit me button		{
			if (p<6)
			{				//***********correctly scores the cards on each hit
				int tempTotal=0;
				int tempVal=0;
				int numAces=0;				if (p<6)  //by using if statement says player can only get a total of 5 cards				{
					p=p+1;
				}				for (int i=0; i<p; i++)				{					if (Player2[i]==1)					{
						tempVal=11;
						numAces=numAces+1; //counts the number of aces to adjest scores later					}
					if (Player2[i]>9)
						tempVal=10;
					if ((Player2[i]>1) && (Player2[i]<10))
						tempVal=Player2[i];
					tempTotal=tempTotal +tempVal; //player2 is the player, player1 the dealer
					if (tempTotal>21 && numAces>0) //if total is over 21 checks to see if any aces					{
						for (int a =0; a<numAces; a++)
						{   //loop counts aces as 1 if sum of cards over 21							tempTotal=tempTotal-10; //says if gone over 21 with aces counting 11 each, take 10 off and count then as 1							//numAces=numAces-1;  //if over and deduct 10, can't do it again so also permanently deduct the ace
					        if (tempTotal <22)    //
								break;						}					}
				}				playerTotal=tempTotal;				if (tempTotal>21)				{
					label7.setText("You Loose");				}
				if (tempTotal<21)  //player gets 21 wins			       label7.setText(String.valueOf(playerTotal));
				//if (tempTotal==21)				//{
				//	label7.setText("You win");
				//}
				if (p==5)    //player gets 5 cards wins...works				{
					label7.setText("You Win");
				}
				if (label7.getText().equals("You Win"))
					PlayerCash=PlayerCash + 100;				if (label7.getText().equals("I Win"))
					DealerCash=DealerCash + 100;				if (label7.getText().equals("You Win") || label7.getText().equals("I Win") )					PotCash=0;
				label4.setText((String.valueOf(PlayerCash)));  //correctly converts an int to a String
				label2.setText((String.valueOf(DealerCash)));
				label6.setText((String.valueOf(PotCash)));
				//**********************end scoringCheckTotal(p);			}
			repaint();  //runs the entire paint command again!!!!
		}
		if (source==button3) //hold button		{
			//label2.setText("goodbye"); // = text1.getText();
			count=count+1;			label7.setText(String.valueOf (playerTotal));			//label7.setText(String.valueOf (count));  //this counts the number o times the button pressed			//label7.setText(String.valueOf (p));  //counts number of cards given player (not computer)			repaint();
		}			//label1.setText("You pressed button 1");
	}
	/*public int CheckTotal(int x) //returns total, idea:  can be used by either player or dealer if made right
	{

			return 1;
	}
	*/

}

